package leetcode.code0670;

import leetcode.IDebug;
import leetcode.helper.H;

public class Solution implements IDebug {

	public int maximumSwap(int num) {
		int[] nums = new int[10];
		int p = 0;
		while (num > 0) {
			nums[p++] = num % 10;
			num /= 10;
		}
		for (int i = p - 1; i > 0; i--) {
			int cur = nums[i];
			int big = cur, index = -1;
			for (int j = i - 1; j >= 0; j--) {
				if (nums[j] >= big) {
					big = nums[j];
					index = j;
				}
			}
			if (index != -1 && nums[i] != nums[index]) {
				nums[i] = nums[index];
				nums[index] = cur;
				break;
			}
		}
		int ans = 0;
		for (int i = p - 1; i >= 0; i--) {
			ans = ans * 10 + nums[i];
		}
		return ans;
	}

	@Override
	public void debug4() {
		H.compare(98863, this.maximumSwap(98368));
		H.compare(773612, this.maximumSwap(273617));

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
